//
//  YeeContainScrollView.m
//  HappyRent
//
//  Created by CoderYee on 2017/4/5.
//  Copyright © 2017年 内圈科技. All rights reserved.
//

#import "YeeContainScrollView.h"

@class YeeContainHeaderView;

@protocol YeeContainHeaderViewdelegate <NSObject>

-(void)clickHeaderView:(YeeContainHeaderView *) headerView atIndex:(NSInteger)index;


@end


@interface YeeContainHeaderView:UIView

@property(nonatomic,retain)UIView  *lineView;
@property(nonatomic,retain)NSMutableArray *buttonArray;
@property(nonatomic,weak)id<YeeContainHeaderViewdelegate>delegate;

- (instancetype)initWithFrame:(CGRect)frame titleArray:(NSArray *)titleArray;

@end

@implementation YeeContainHeaderView


- (instancetype)initWithFrame:(CGRect)frame titleArray:(NSArray *)titleArray{
   
    if (self = [super initWithFrame:frame]) {
        _buttonArray = [NSMutableArray array];
        for (int i = 0; i<titleArray.count;i++) {
            CGFloat buttonWidth = kMainScreenWidth/titleArray.count;
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(buttonWidth*i, 0, buttonWidth, frame.size.height-3);
            [button setTitle:titleArray[i] forState:UIControlStateNormal];
            [button setTitleColor:kNavBarThemeColor forState:UIControlStateSelected];
            [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
            button.tag = i;
            button.titleLabel.font = [UIFont systemFontOfSize:14];
            [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:button];
            [_buttonArray addObject:button];
            if (i == 0) {
                button.selected = YES;
                _lineView = [[UIView alloc] initWithFrame:CGRectMake(button.mj_w/3/2, button.frame.size.height, button.mj_w/3*2, 2)];
                _lineView.backgroundColor = kNavBarThemeColor;
                [self addSubview:_lineView];
            }
        }
//        for (int i = 0; i < _buttonArray.count -1; i++) {
//            UIButton *button = _buttonArray[i];
//            UIView *smallLineView = [UIView new];
//            smallLineView.backgroundColor = [UIColor colorWithWhite:0.876 alpha:1.000];
//            smallLineView.frame = CGRectMake(CGRectGetMaxX(button.frame)-0.5, (frame.size.height-20)/2, 1, 20);
//            [self addSubview:smallLineView];
//        }
        UIView *lineView = [UIView new];
        lineView.backgroundColor = [UIColor colorWithWhite:0.876 alpha:1.000];
        lineView.frame = CGRectMake(0, frame.size.height-1, kMainScreenWidth, 1);
        [self addSubview:lineView];
    }
    return self;
}
#pragma mark 点击按钮事件
- (void)buttonClick:(UIButton *)button{
    
    for (UIButton *btn in _buttonArray) {
        
        btn.selected = NO;
    }
    button.selected = YES;
    [UIView animateWithDuration:0.5 animations:^{
        _lineView.frame = CGRectMake(CGRectGetMinX(button.frame) + button.mj_w /3/2, button.frame.size.height, button.mj_w/3*2, 2);
        
    }];
    
    if (self.delegate&&[self.delegate respondsToSelector:@selector(clickHeaderView:atIndex:)])
    {
        [self.delegate clickHeaderView:self atIndex:button.tag];
    }
    
}
- (void)setLineViewFrameWithClickIndex:(NSInteger)index{
    
    for (UIButton *btn in _buttonArray)
    {
        btn.selected = NO;
    }
    UIButton *button = _buttonArray[index];
    button.selected = YES;
    [UIView animateWithDuration:0.5 animations:^{
        _lineView.frame = CGRectMake(CGRectGetMinX(button.frame) + button.mj_w /3/2, button.frame.size.height, button.mj_w/3*2, 2);
        
    }];
}
@end

@interface YeeContainScrollView ()<UIScrollViewDelegate,YeeContainHeaderViewdelegate>

@property(nonatomic,retain)UIScrollView *scrollView;
@property(nonatomic,retain)NSArray      *subViews;
@property(nonatomic,assign)CGFloat      headerHeight;
@property(nonatomic,retain)YeeContainHeaderView       *headerView;



@end

@implementation YeeContainScrollView

-(instancetype)initWithFrame:(CGRect)frame
                   withViews:(NSArray <UIView *>*)views
                  withTitles:(NSArray <NSString *>*)titles{
    
    if (self=[super initWithFrame:frame]) {
        
        _headerHeight=35;
        [self addSubview:self.scrollView];
        self.subViews=views;
        [self makeViews:views];
        [self makeheaderView:titles];
    }
    return self;
}
-(void)makeheaderView:(NSArray *)titles{
    
    YeeContainHeaderView *headerView=[[YeeContainHeaderView alloc ] initWithFrame:CGRectMake(0, 0, kMainScreenWidth, _headerHeight) titleArray:titles];
    headerView.delegate=self;
    [headerView setBackgroundColor:kWhiteColor];
    _headerView=headerView;
    [self addSubview:headerView];
    
}
-(void)clickHeaderView:(YeeContainHeaderView *) headerView atIndex:(NSInteger)index{

    [self.scrollView setContentOffset:CGPointMake(index*kMainScreenWidth, 0) animated:YES];
    
}
#pragma mark make subViews
-(void)makeViews:(NSArray *)views{
   
    for (NSInteger i=0; i<views.count; i++) {
        
        UIView *view=(UIView*)views[i];
        [view setFrame:CGRectMake(kMainScreenWidth*i, 0, kMainScreenWidth, CGRectGetHeight(self.frame))];
        [self.scrollView addSubview:view];
    }
    [self.scrollView setContentSize: CGSizeMake(kMainScreenWidth*views.count, 0)] ;
}
#pragma mark UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
   
    NSInteger currentIndex=(scrollView.contentOffset.x+kMainScreenWidth*0.5)/kMainScreenWidth;
    [_headerView setLineViewFrameWithClickIndex:currentIndex];
}
//刷新数据
-(void)reloadData{
    
    
}
#pragma mark lazy method
-(NSArray *)subViews{
   
    if (_subViews==nil) {
        
        _subViews=[[NSArray alloc] init];
    }
    return _subViews;
}
-(UIScrollView *)scrollView{
    
    if (_scrollView==nil) {
        _scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, _headerHeight, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame)-_headerHeight)];
        _scrollView.showsVerticalScrollIndicator=NO;
        _scrollView.showsHorizontalScrollIndicator=NO;
        _scrollView.bounces=NO;
        _scrollView.delegate=self;
        _scrollView.pagingEnabled=YES;
    }
    return _scrollView;
}
@end
